/*
Copyright (c) 2009, Young Digital Planet SA
All rights reserved.

* Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this 
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, 
  this list of conditions and the following disclaimer in the documentation 
  and/or other materials provided with the distribution.
* Neither the name of the Young Digital Planet SA nor the names of its 
  contributors may be used to endorse or promote products derived from this 
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
package pl.ydp.nexl.impl
{
	import flexunit.framework.TestCase;

	public class TestNexlAttributes extends TestCase
	{
		public function TestNexlAttributes(methodName:String=null)
		{
			super(methodName);
		}
		
		public function testExtractValueFromAttr():void {
			var data:XML = <attr value="1234" />;
			
			var atts:NexlAttributes = new NexlAttributes();
			assertEquals( "Attribute value from @value", "1234", atts.extractValue(data) );
		}
		
		public function testExtractValueFromXML():void {
			var data:XML = <attr>to jest<b>xml</b></attr>;
			
			var atts:NexlAttributes = new NexlAttributes();
			assertEquals( "Attribute value from text", "to jest\n<b>xml</b>", atts.extractValue( data ) );
		}
		
		public function testExtractValueFromCDATA():void {
			var data:XML = <attr><![CDATA[to jest <?> xml]]></attr>;
			
			var atts:NexlAttributes = new NexlAttributes();
			assertEquals( "Attribute value from text", "to jest <?> xml", atts.extractValue( data ) );
		}
		
		public function testExtractValueFromTEXT():void {
			var data:XML = <attr>to jest xml</attr>;
			
			var atts:NexlAttributes = new NexlAttributes();
			assertEquals( "Attribute value from text", "to jest xml", atts.extractValue( data ) );
		}
		
		public function testLoadData():void {
			var data:XML = <ext>
								<attr name="a1" value="v1"/>
								
								<attr name="a2" value="v2"/>
								
								<attr name="a1" value="v3"/>
								
								<attr name="a3" value="v4"/>
								<attr name="a3" value="v5"/>
								<attr name="a3" value="v6"/>
							</ext>;
			
			var atts:NexlAttributes = new NexlAttributes();
			atts.loadData( data );
			var aObj:Object = atts.atts;
			assertTrue( "a1 is array", aObj.a1 is Array );
			assertEquals( '<attr name="a1" value="v1"/>', "v1", aObj.a1[0].value );
			assertEquals( '<attr name="a1" value="v3"/>', "v3", aObj.a1[1].value );
			assertEquals( '<attr name="a2" value="v2"/>', "v2", aObj.a2.value );
			
			assertTrue( "a3 is array", aObj.a3 is Array );
			assertEquals( '<attr name="a3" value="v4"/>', "v4", aObj.a3[0].value );
			assertEquals( '<attr name="a3" value="v5"/>', "v5", aObj.a3[1].value );
			assertEquals( '<attr name="a3" value="v6"/>', "v6", aObj.a3[2].value );
		}
		
		public function testGetAttValue():void {
			var data:XML = <ext>
								<attr name="a1" value="v1"/>
								
								<attr name="a2" value="v2"/>
								
								<attr name="a1" value="v3"/>
								
								<attr name="a3" value="v4"/>
								<attr name="a3" value="v5"/>
								<attr name="a3" value="v6"/>
							</ext>;
			
			var atts:NexlAttributes = new NexlAttributes();
			atts.loadData( data );
			
			assertEquals( "attr a2 value", "v2", atts.getAttributeValues("a2") );
			assertEquals( "attr a1 value", "v1,v3", atts.getAttributeValues("a1").toString() );
			assertEquals( "attr a1 value 0", "v1", atts.getAttributeValues("a1")[0] );
			assertEquals( "attr a1 value 1", "v3", atts.getAttributeValues("a1")[1] );
		}
		
		public function testGetAttString():void {
			var data:XML = <ext>
								<attr name="a1" value="v1"/>
								
								<attr name="a2" value="v2"/>
								
								<attr name="a1" value="v3"/>
								
								<attr name="a3" value="v4"/>
								<attr name="a3" value="v5"/>
								<attr name="a3" value="v6"/>
							</ext>;
			
			var atts:NexlAttributes = new NexlAttributes();
			atts.loadData( data );
			
			assertEquals( "attr a2 value", "v2", atts.getAttributeString("a2") );
			assertEquals( "attr a3 value", "v4", atts.getAttributeString("a3") );
			assertNull( "not existing attr value", atts.getAttributeString("a88") );
		}
		
		public function testGetAttNumber():void {
			var data:XML = <ext>
								<attr name="a1" value="1"/>
								
								<attr name="a2" value="2"/>
								
								<attr name="a1" value="3"/>
								
								<attr name="a3" value="v4"/>
								<attr name="a3" value="v5"/>
								<attr name="a3" value="v6"/>
							</ext>;
			
			var atts:NexlAttributes = new NexlAttributes();
			atts.loadData( data );
			
			assertEquals( "attr a2 value", 2, atts.getAttributeNumber("a2") );
			assertEquals( "attr a1 value", 1, atts.getAttributeNumber("a1") );
			assertTrue( "not existing attr value", isNaN(atts.getAttributeNumber("a88")) );
			assertTrue( "not number attr value", isNaN(atts.getAttributeNumber("a3")) );
		}
		
		public function testHasAttValue():void {
			var data:XML = <ext>
								<attr name="a1" value="v1"/>
								
								<attr name="a2" value="v2"/>
								
								<attr name="a1" value="v3"/>
								
								<attr name="a3" value="v4"/>
								<attr name="a3" value="v5"/>
								<attr name="a3" value="v6"/>
							</ext>;
			
			var atts:NexlAttributes = new NexlAttributes();
			atts.loadData( data );
			
			assertTrue( "attr a2 has value v2", atts.attributeHasValue("a2","v2") );
			assertFalse( "attr a2 has value v3", atts.attributeHasValue("a2","v3") );
			assertTrue( "attr a3 has value v5", atts.attributeHasValue("a3","v5") );
			assertFalse( "attr a3 has value v3", atts.attributeHasValue("a3","v3") );
		}
	}
}